﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Data.SqlServerCe;
using System.Data.SqlClient;
using System.Threading;
using u = FinPlusUtility.Utility;
using FinPlusCommon;
using FinPlusDBConn;

namespace Test_FinPlusDBConn
{
    using NUnit.Framework;

    class Test_FinPlusDBConn
    {
        static void Main(string[] args)
        {
            var TextFixture1 = new TextFixture1();
            TextFixture1.Test1();
            TextFixture1.Test2();
            TextFixture1.Test3();
            TextFixture1.Test4();

            var TextFixture2 = new TextFixture2();
            TextFixture2.Test1();
            TextFixture2.Test2();

            var TextFixture3 = new TextFixture3();
            TextFixture3.Test1();

            var TextFixture4 = new TextFixture4();
            TextFixture4.Test1();
            TextFixture4.Test2();


            Console.ReadLine();
        }
    }

    [TestFixture]// basic add and select, selectAsOf, select from
    class TextFixture1
    {
        const string map = @"-1$Id$Unbooked$Int32$0.0$0$norton%-1$FunctionName$FunctionMap$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Group$Trade$String$0.0$0$norton%-1$Name$VanillaSwapTrade$String$0.0$0$norton";
        const string funcStr = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";

        [Test]// add two func use select check has two funcs
        public void Test1()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);

            var q = conn.Select("Trade", string.Empty, ConType.None);
  
            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 2);
            Console.WriteLine("TextFixture1.Test1 " + q.Functions.Count + " = 2");
        }

        [Test]// add three funcs close one check select has two funcs
        public void Test2()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create funcs
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            //close func
            res = conn.Close(f2);

            var q = conn.Select("Trade", string.Empty, ConType.None);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 2);
            Console.WriteLine("TextFixture1.Test2 " + q.Functions.Count + " = 2");
        }

        [Test]// add three funcs wait then add one func selectfrom time at adding last func check query returns 1 funcs
        public void Test3()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for function
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr));
            var f4 = new Function((object[,])u.StringToArray2d(funcStr));

            //save functions
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            Thread.Sleep(2000);
            var lastTimeStamp = DateTime.Now;
            res = conn.Save(f4);

            //close function
            var q = conn.SelectFrom("Trade", lastTimeStamp, "", ConType.None);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 1);
            Console.WriteLine("TextFixture1.Test3 " + q.Functions.Count + " = 1");
        }

        [Test]// add three funcs delete one wait then add one func selectasof time at adding last func check query returns 2 funcs
        public void Test4()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for function
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr));
            var f4 = new Function((object[,])u.StringToArray2d(funcStr));

            //save functions
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            res = conn.Close(f2);

            Thread.Sleep(2000);
            var lastTimeStamp = DateTime.Now;

            Thread.Sleep(2000);
            res = conn.Save(f4);

            //close function
            var q = conn.SelectAsOf("Trade", lastTimeStamp, "", ConType.None);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 2);
            Console.WriteLine("TextFixture1.Test4 " + q.Functions.Count + " = 2");
        }

        private string CreateDB()
        {
            var p = System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower().Replace(".exe", ".sdf");
            if (File.Exists(p)) File.Delete(p);
            string connStr = string.Format("Data Source = {0}; Password = FinPlus", p);
            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();
            return connStr;
        }
    }

    [TestFixture]//select where
    class TextFixture2
    {
        const string map = @"-1$Id$Unbooked$Int32$0.0$0$norton%-1$FunctionName$FunctionMap$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Group$Trade$String$0.0$0$norton%-1$Name$VanillaSwapTrade$String$0.0$0$norton";
        const string funcStr1 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";
        const string funcStr2 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT002$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";
        const string funcStr3 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$BondTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT002$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";

        [Test]
        public void Test1()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr1));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr2));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr2));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            var q = conn.Select("Trade", "(Name = 'Book' AND Value = 'BOGT002') OR (Name = 'FunctionName' AND Value = 'VanillaSwapTrade')", ConType.None);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 3);
            Console.WriteLine("TextFixture2.Test1 " + q.Functions.Count + " = 3");
        }

        [Test]
        public void Test2()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr1));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr2));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr3));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            var q = conn.Select("Trade", "(Name = 'Book' AND Value = 'BOGT002') OR (Name = 'FunctionName' AND Value = 'BondTrade')", ConType.None);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 1);
            Console.WriteLine("TextFixture2.Test2 " + q.Functions.Count + " = 1");
        }

        private string CreateDB()
        {
            var p = System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower().Replace(".exe", ".sdf");
            if (File.Exists(p)) File.Delete(p);
            string connStr = string.Format("Data Source = {0}; Password = FinPlus", p);
            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();
            return connStr;
        }

    }

    [TestFixture]//continous
    class TextFixture3
    {
        const string map = @"-1$Id$Unbooked$Int32$0.0$0$norton%-1$FunctionName$FunctionMap$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Group$Trade$String$0.0$0$norton%-1$Name$VanillaSwapTrade$String$0.0$0$norton";
        const string funcStr1 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";
        const string funcStr2 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";
        const string funcStr3 = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";

        [Test]
        public void Test1()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            var q = conn.Select("Trade", "", ConType.NewAndAmend);

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr1));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr1));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr1));
            
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            Thread.Sleep(3000);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 3);
            Console.WriteLine("TextFixture3.Test1 " + q.Functions.Count + " = 3");
        }

        private string CreateDB()
        {
            var p = System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower().Replace(".exe", ".sdf");
            if (File.Exists(p)) File.Delete(p);
            string connStr = string.Format("Data Source = {0}; Password = FinPlus", p);
            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();
            return connStr;
        }

    }

    [TestFixture]// select continous
    class TextFixture4
    {
        const string map = @"-1$Id$Unbooked$Int32$0.0$0$norton%-1$FunctionName$FunctionMap$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Group$Trade$String$0.0$0$norton%-1$Name$VanillaSwapTrade$String$0.0$0$norton";
        const string funcStr = @"-1$Id$Unbooked$String$0.0$0$norton%-1$FunctionName$VanillaSwapTrade$String$0.0$0$norton%-1$TimeStamp$0.0$DateTime$0.0$0$norton%-1$Book$BOGT001$String$0.0$0$norton%-1$Counterparty$Barter Plus Ltd$String$0.0$0$norton%-1$TempId$spot5YearSwap$String$0.0$0$norton%-1$Nominal$1000000$Double$0.0$0$norton%-1$Start$38252$DateTime$0.0$0$norton%-1$Maturity$40077$DateTime$0.0$0$norton%-1$FixedRate$0.04$Double$0.0$0$norton%-1$PayRec$Payer$String$0.0$0$norton%-1$Index$EURCurve$String$0.0$0$norton%-1$Spread$0$Double$0.0$0$norton%-1$FixLegFrq$Annual$String$0.0$0$norton%-1$FltLegFrq$Semiannual$String$0.0$0$norton%-1$FixLegConv$Unadjusted$String$0.0$0$norton%-1$FltLegConv$ModifiedFollowing$String$0.0$0$norton%-1$FixLegDayCount$Euro30/360$String$0.0$0$norton%-1$FltLegDayCount$Act/360$String$0.0$0$norton%-1$Holidays$TGT$String$0.0$0$norton";

        [Test]// add two func use select check has two funcs
        public void Test1()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            var q = conn.Select("Trade", string.Empty, ConType.NewAndAmend);

            //create functions
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);

            Thread.Sleep(3000);

            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 2);
            Console.WriteLine("TextFixture1.Test1 " + q.Functions.Count + " = 2");
        }

        [Test]// add three funcs close one check select has two funcs
        public void Test2()
        {
            //create db & connect
            var conn = new FinPlusDBConn.FinPlusConn(CreateDB());
            var res = string.Empty;

            //create map for func
            res = conn.Save(new Function((object[,])u.StringToArray2d(map)));

            var q = conn.Select("Trade", string.Empty, ConType.NewAndAmend);

            //create funcs
            var f1 = new Function((object[,])u.StringToArray2d(funcStr));
            var f2 = new Function((object[,])u.StringToArray2d(funcStr));
            var f3 = new Function((object[,])u.StringToArray2d(funcStr));

            //save funcs
            res = conn.Save(f1);
            res = conn.Save(f2);
            res = conn.Save(f3);

            //close func
            res = conn.Close(f2);

            Thread.Sleep(3000);
         
            conn.Dispose();

            Assert.AreEqual(q.Functions.Count, 2);
            Console.WriteLine("TextFixture1.Test2 " + q.Functions.Count + " = 2");
        }

   
        [Test]// add three funcs delete one wait then add one func selectasof time at adding last func check query returns 2 funcs

        private string CreateDB()
        {
            var p = System.Reflection.Assembly.GetExecutingAssembly().Location.ToLower().Replace(".exe", ".sdf");
            if (File.Exists(p)) File.Delete(p);
            string connStr = string.Format("Data Source = {0}; Password = FinPlus", p);
            SqlCeEngine engine = new SqlCeEngine(connStr);
            engine.CreateDatabase();
            return connStr;
        }

    }

}
